package com.ehotting.edsta.diagnosisPredict.controller;

import com.alibaba.fastjson.JSONObject;
import com.ehotting.eaf.core.domain.R;
import com.ehotting.eaf.core.utils.NumberHelper;
import com.ehotting.eaf.security.service.TokenService;
import com.ehotting.edsta.common.log.annotation.Log;
import com.ehotting.edsta.common.log.enums.BusinessType;
import com.ehotting.mqbd.api.MqActionConstants;
import com.ehotting.mqbd.api.bean.CommonRequest;
import com.ehotting.mqbd.api.bean.CommonResponse;
import com.ehotting.mqbd.api.bean.MQHttpData;
import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.core.service.MQService;
import com.ehotting.mqbd.core.manager.AgentLinkManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RequestMapping("/rePostDp")
@RestController
@Api(tags = "RePostController",description = "算法诊断请求转发Controller")
public class RePostController {

    private final long HTTP_TIMEOUT = 60 * 20;

    @Resource(name="mqService")
    private MQService mqService;

    @Autowired
    private MqApplicationContext mqApplicationContext;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private AgentLinkManager  agentLinkManager;


    @Log(title = "算法诊断模块", businessType = BusinessType.OTHER)
    @ApiOperation("算法诊断请求包转发")
    @PostMapping("/rePost")
    public R<CommonResponse> rePost(@RequestBody CommonRequest request) {

        Long userId = tokenService.getLoginUser().getUserid();
        String mainProjectId = NumberHelper.isDigital(request.getMainProjectId())?request.getMainProjectId():"0";
        String agentId = agentLinkManager.genAgentId(mqApplicationContext.getModule(),userId,mainProjectId);

        MQHttpData data = MQHttpData.create(
                TopicEnum.CONCURRENCY,
                mqApplicationContext,agentId,
                "");
        data.setAction(MqActionConstants.RE_POST);
        data.setRequest(request);
        data.setAuthorization( tokenService.createJwt(request.getAccessEntranceToken()));
        MQHttpData response = (MQHttpData) mqService.request(data, HTTP_TIMEOUT);

        return R.ok(response.getResponse());
    }

    @Log(title = "算法诊断模块文件上传", businessType = BusinessType.OTHER, isSaveRequestData = false)
    @ApiOperation("算法诊断上传文件的请求包转发")
    @PostMapping("/rePostUpload")
    public R<CommonResponse> rePostUpload(@RequestBody CommonRequest request) {

        Long userId = tokenService.getLoginUser().getUserid();

        String mainProjectId = NumberHelper.isDigital(request.getMainProjectId())?request.getMainProjectId():"0";
        String agentId = agentLinkManager.genAgentId(mqApplicationContext.getModule(),userId,mainProjectId);

        MQHttpData data = MQHttpData.create(
                TopicEnum.CONCURRENCY,
                mqApplicationContext,agentId,
                "");
        data.setAction(MqActionConstants.RE_UPLOAD);
        data.setRequest(request);
        data.setAuthorization( tokenService.createJwt(request.getAccessEntranceToken()));
        MQHttpData response = (MQHttpData) mqService.request(data, HTTP_TIMEOUT);

        return R.ok(response.getResponse());
    }
}
